<?php
/**
* SocialEngine
*
* @category   Application_Extensions
* @package    Flashgame
* @copyright  2010 YouNet
*/

class Flashgame_AdminPaymentController extends Core_Controller_Action_Admin
{
    protected $_navigation;
    public function indexAction()
    {

    }

    public function settingsAction()
    {
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
        ->getNavigation('flashgame_admin_main', array(), 'flashgame_admin_main_payment');

        // old file
        $table  = Engine_Api::_()->getDbTable('paymentgateways', 'flashgame');
        $select = $table->select()
        ->where('paymentgateway_typename = ?', 'paypal');
        $row = $table->fetchRow($select);
        $paymentgw = array();

        $paymentgw_id = $row['paymentgateway_id'];        
        $form = new Flashgame_Form_Admin_Paymentsettings();   
        $form->paymentgateway_enabled->setValue($row['paymentgateway_enabled']); 

        $form->paypal_business->setValue($row["paymentgateway_business"]); 
        /*
        $form->paypal_api_username->setValue($paypal_api_username); 
        $form->paypal_api_password->setValue($paypal_api_password); 
        $form->paypal_api_signature->setValue($paypal_api_signature); 
        */
        $form->paypal_sandbox->setValue(isset($row['paymentgateway_sandbox']) ? $row['paymentgateway_sandbox'] : 0); 
        //$form->paypal_api_ipnurl->setValue($this->view->baseUrl()."/admin/payment/ipn");   
        $this->view->form = $form;   
        // Check post
        if( $this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost()) )
        {
            // Ok, we're good to add field
            $values = $form->getValues();
            //$flashgame->setFromArray($values);
            $db = Engine_Db_Table::getDefaultAdapter();
            $db->beginTransaction();
            try
            {
                $row->paymentgateway_enabled = $values["paymentgateway_enabled"];  
                $row->paymentgateway_sandbox = isset($_GET['paypal_sandbox']) ? $_GET ['paypal_sandbox'] : (isset($_POST['paypal_sandbox']) ? $_POST['paypal_sandbox'] : 0);
                $row->paymentgateway_business = $values["paypal_business"];
                $row->save();
                $db->commit();

            }

            catch( Exception $e )
            {
                $db->rollBack();
                throw $e;
            }
            return $this->_redirect("admin/flashgame/payment/settings");

        }
    }


    public function clientsAction()
    {
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
        ->getNavigation('flashgame_admin_main', array(), 'flashgame_admin_main_payment_clients');
        // old file    
        //$this->view->clients = $clients = Engine_Api::_()->flashgame()->getClients();
        //$this->view->page = $page = $this->_getParam('page',1);    
        $this->view->clients = Engine_Api::_()->flashgame()->getClientsPaginator(array(
        'orderby' => 'clients_id',
        ));

        $this->view->clients->setItemCountPerPage(10);

        $currentpage = $this->_getParam("page",1);
        $this->view->page = $page = $currentpage;

        $this->view->clients->setCurrentPageNumber($currentpage);
        $this->view->totalclients = Engine_Api::_()->flashgame()->getTotalClients();

    }
    public function incomesAction()
    {
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
        ->getNavigation('flashgame_admin_main', array(), 'flashgame_admin_main_payment_clients');
        // old file    
        //$this->view->clients = $clients = Engine_Api::_()->flashgame()->getClients();
        //$page=$this->_getParam('page',1);

        $this->view->paginator = $paginator = Engine_Api::_()->flashgame()->getIncomesPaginator($this->_getParam("client_id"));

        $this->view->paginator->setItemCountPerPage(10);
        $currentpage = $this->_getParam("page",1);

        $this->view->paginator->setCurrentPageNumber($currentpage);
    }
}
